package jdtoextension;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.juancavallotti.jdto.SinglePropertyValueMerger;

/**
 * 
 * @param <T>
 * @param <S>
 */
public class ListPropertyMerger implements SinglePropertyValueMerger<List, List> {

    @Override
    public List mergeObjects(final List listValue, final String extraParam) {
        List result = new ArrayList();

        for (Object o : listValue) {
            try {
                Field field = o.getClass().getDeclaredField(extraParam);
                field.setAccessible(true);
                result.add(field.get(o));

            } catch (IllegalAccessException e) {
                throw new UnsupportedOperationException(e);
            } catch (NoSuchFieldException e) {
                throw new UnsupportedOperationException(e);
            }

        }
        return result;
    }

}
